package test.file;

import static test.file.ReportProcessorConstant.ELEMENT_TYPE;
import static test.file.ReportProcessorConstant.ELEMENT_TYPE_FILE;
import static test.file.ReportProcessorConstant.FILE_FULL_PATH;
import static test.file.ReportProcessorConstant.LINE_NO;
import static test.file.ReportProcessorConstant.METHOD_NAME;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import com.google.common.base.Joiner;

public class NoReferenceInfoReader implements ReportInfoReader {
	@Override
	public List<Map> readReport(File file, String encoding, String rootPath) throws Exception {
		List<Map> result = new ArrayList<>();
		String rawContent = FileUtils.readFileToString(file, encoding);
		String[] split = rawContent.split("\r\n|\r|\n");
		System.out.println("长度:" + split.length);
		// 匹配方法信息
		// Pattern regex = Pattern.compile("Class \"([^\"]+).+?used locally\\s+([^/\\s]+)(.+?)line\\s+(\\d+)");
		Pattern regex = Pattern.compile("Class \"([^\"]+)\"\\s+has 0 references\\s+(.*\\.java)\\s+([^\\s+]+)");
		Matcher matcher = regex.matcher(rawContent);
		while (matcher.find()) {
			Map map = new HashMap<>();
			String g0 = matcher.group(0);
			System.out.println("g0");
			System.out.println(g0);
			String g1 = matcher.group(1);
			System.out.println("g1");
			System.out.println(g1);
			String g2 = matcher.group(2);
			System.out.println("g2");
			System.out.println(g2);
			 String g3 = matcher.group(3);
			 System.out.println("g3");
			 System.out.println(g3);
			// String g4 = matcher.group(4);
			// System.out.println("g4");
			// System.out.println(g4);
			//
			// file full path
			String fileFolder = matcher.group(3).trim();
			String fileName = matcher.group(2).trim();
			System.out.println(fileName);
			String filefullPath = Joiner.on(File.separator).join(rootPath,fileFolder,fileName).replaceAll("\\/{2,}", "/");
			System.out.println("filefullPath "+filefullPath);
			map.put(FILE_FULL_PATH,filefullPath);
//			System.exit(1);
			// method name
			// System.out.println("Method Name: ");
			// String methodName = matcher.group(1);
			// System.out.println(methodName);
			// map.put(METHOD_NAME, methodName.trim());
			// relative path
			// String filePath = rootPath + matcher.group(3).trim() + File.separator + matcher.group(2).trim();
			// System.out.println(filePath);
			// map.put(FILE_FULL_PATH, filePath);
			// line no.
			// String lineNo = matcher.group(4);
			// System.out.println(lineNo);
			// map.put(LINE_NO, lineNo);
			// element type
			map.put(ELEMENT_TYPE, ELEMENT_TYPE_FILE);
			result.add(map);
			System.out.println("");
		}
		return result;
	}

	@Override
	public MultiValueMap<String, String> orgnizeReport(List<Map> readReport, String rootPath) {
		MultiValueMap<String, String> files = new LinkedMultiValueMap<>();
		for (Iterator iterator = readReport.iterator(); iterator.hasNext();) {
			Map map = (Map) iterator.next();
			String filefullPath = (String) map.get(FILE_FULL_PATH);
			files.add(filefullPath, "");
		}
		return files;
	}
}
